package io.seata.samples.integration.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.spring.annotation.GlobalTransactionScanner;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class SeataAuotConfig {

    /**
     * init datasource proxy
     * @Param: druidDataSource  datasource bean instance
     * @Return: DataSourceProxy  datasource proxy
     */
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
            // 订单服务中引入了mybatis-plus，所以要使用特殊的SqlSessionFactoryBean
            MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
            // 代理数据源
            sqlSessionFactoryBean.setDataSource(new DataSourceProxy(dataSource));
            // 生成SqlSessionFactory
        System.out.println("===="+dataSource+"======"+sqlSessionFactoryBean.getObject());
            return sqlSessionFactoryBean.getObject();

    }
}
